﻿$(function () {
    var openId = $("#WxOpenId").val();
    UserInfoMgr.setOpenId(openId, function (openId) {
        if (openId == null || openId == "") {
            //获取用户信息失败
            venderDialog("获取用户信息失败，请关闭浏览器重试。");
        } else {
            UserInfoMgr.weixinLogin(loginShow, logoutCallback);
        }
    });
});

function loginShow() {
    $('.UserInfo_head').html('<a><img src="/images/icon_login.png" class="UserInfoHeadImg" alt="头像" /></a>');
    var userName = UserInfoMgr.getUserID();
    $('.UserInfo_Number').text(userName);
    var unpickNum = UserInfoMgr.getUnpickNum();
    showunpickNum(unpickNum);
    $("article.main").show();
}

function showunpickNum(unpickNum) {
    if (unpickNum > 0) {
        $('#unpickedOrderNum').addClass("has_unpicked_num");
    } else {
        $('#unpickedOrderNum').addClass("no_unpicked_num");
    }
    var unpickHelp = unpickNum + '个未收货';
    $('#unpickedOrderNum').text(unpickHelp);
}
//用户跳转去绑定
function logoutCallback() {
    window.location.href = spliceRegisterUrl('/UserCenter/Index', '/Home/Index');
}

function feedback() {
    window.location.href = '/Feedback/Index';
}

function aboutInfo() {
    window.location.href = '/About/Index';
}
//我的订单
function getOrderList() {
    if (UserInfoMgr.isLogIn()) {
        window.location.href = '/OrderMgr/Index';
    } else {
        window.location.href = spliceRegisterUrl('/OrderMgr/Index', '/UserCenter/index');
    }
}

var userCenterWaitTimes = 10;
var userCenterWaitIndex = 0;

function LocationWait() {
    if (userCenterWaitIndex < userCenterWaitTimes) {
        getWXAddress(function (name, addr, lat, log) {
            if ((null != lat) && (null != log) && (0 != Number(lat)) && (0 != Number(log))) {
                AddressMgr.setWXAddr(name, addr, lat, log);
                locationPoint(function () {
                    window.location.href = '/Home/Index';
                });
            } else {
                userCenterWaitIndex++;
                window.setTimeout("LocationWait()", 1000);
            }
        });
    } else {
        venderDialog("获取用户地址失败，请检查是否开启提供位置信息功能。");
    }
}

function turnBack() {
    var lat = AddressMgr.getCurLat();
    var log = AddressMgr.getCurLog();
    if (null != lat && null != log) {
        window.location.href = '/Home/Index';
    } else {
        userCenterWaitIndex = 0;
        LocationWait();
    }
    //window.history.back();
}